#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QDirIterator>
#include <QJsonArray>
#include <QJsonObject>
#include <QNetworkReply>
#include <QThread>
#include <QTimer>
#include <QUuid>
#include <ctkPluginException.h>
#include <ctkPluginFramework.h>
#include <ctkPluginFrameworkFactory.h>
#include <ctkPluginFrameworkLauncher.h>
#include <service/event/ctkEventConstants.h>

MainWindow::MainWindow(ctkPluginContext *context, QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_eventAdmin(nullptr),
    m_context(context)
{
    ui->setupUi(this);
    //插件管理器
    ctkServiceReference pluginAdminRef = context->getServiceReference<PluginAdmin>();
    m_pluginAdmin = qobject_cast<PluginAdmin *>(context->getService(pluginAdminRef));
    if(nullptr == m_pluginAdmin)
    {
        qDebug() << Q_FUNC_INFO << "获取插件管理器失败！";
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_SendEvent_clicked()
{
    //postEvent 异步通信demo
    ctkServiceReference eventAdminRef = m_context->getServiceReference<ctkEventAdmin>();
    m_eventAdmin = qobject_cast<ctkEventAdmin*>(m_context->getService(eventAdminRef));
    ctkProperties props;
    props.insert("username", "Desktop插件");
    ctkEvent event("topic/subscriber_publisher_demo", props);
    if(nullptr != m_eventAdmin)
        m_eventAdmin->postEvent(event);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    m_pluginAdmin->closeAllPlugin();
}
